﻿#!/usr/bin/env python3

class Logo:
    def __init__(self, name):
        self.name = name
    def __get__(self, instance, owner):
        return self.name + (instance._logo1 if "main" in self.name else instance._logo2)
    def __set__(self, instance, value):
        if "main" in self.name:
            instance._logo1 = value
        else:
            instance._logo2 = value
    def __delete__(self, instance):
        if "main" in self.name:
            del instance._logo1
        else:
            del instance._logo2

class Car:
    logo1 = Logo("main: ")
    logo2 = Logo("sub: ")
    def __init__(self):
        self._logo1="none"
        self._logo2="none"
        pass



    
def main():
    '''
descriptor -
descriptor是至少具备下面__get__方法的类, 
class Descritpr:
    def __get__(self, instance, owner):
        pass
    def __set__(self, instance, value):
        pass
    def __delete__(self, instance):
        pass
含__set__的称作data descriptor, 即可设置的. 特别注意若不含__set__, 则意味着可以在instance中添加同名属性.
一个类(下面的C)的属性(A)可以使用descriptor(D)定义, 注意是类属性. 如下
class D:
    def __get__(self, instance, owner):
        pass
    def __set__(self, instance, value):
        pass
    def __delete__(self, instance):
        pass
class C:
    A=D()

那么,
    self是指class D的instance, 即descriptor实例
    instance是指class C的实例, 即属性的class owner的实例
    owner是指class C, 是class owner.

    '''
    print(main.__doc__)
    car = Car()
    car2 = Car()
    print(car.logo1)
    print(car.logo2)
    car.logo1 = "chery"
    car.logo2 = "QQ"
    car2.logo1 = "faw"
    car2.logo2 = "RedFlag"
    print(car.logo1)
    print(car.logo2)
    print(car2.logo1)
    print(car2.logo2)


if __name__=='__main__':
    main()
